/*
Copyright (C) 2014  Johan Degraeve

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program.  If not, see <http://www.gnu.org/licenses/gpl.txt>.

*/

package helpdiabetesj2me;

import javax.microedition.lcdui.*;

/**
 *
 * @author Johan
 */
public class InitialScreen extends MyCanvas implements CommandListener {

        static final String[] str_ok = {"Ok"};
        public static final String[]str_startmessage1 = {"Copyright (C) 2014  Johan Degraeve"};
        public static final String[]str_startmessage2 = {"This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation"};
        public static final String[]str_startmessage3 = {"| either version 3 of the License| or (at your option) any later version. "};
        public static final String[]str_startmessage4 = {"This program is distributed in the hope that it will be useful| but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more details."};
        public static final String[]str_startmessage5 = {"You should have received a copy of the GNU General Public License along with this program."};
        public static final String[]str_startmessage6 = {"If not| see <http://www.gnu.org/licenses/gpl.txt>."};
 	static final String[] str_afsl = {"Afsluiten"};
        static final String[] str_afsl_short = {"Exit"};
        public static final String[] str_licenseagreement = {"Licentieovereenkomst"};


    private Command exit;
    private Command ok;

    boolean start_message_completely_shown;
    boolean bottom_reached;
    int offset;

    public InitialScreen(Font userfont,HelpDiabetes midlet)  {
        super(userfont,midlet);
        enteredstring = null;

        //first sleep a period, to make sure screen is swapped to getitemfromuser
        try {
           Thread.sleep(250);
        } catch (InterruptedException ex) {
            ex.printStackTrace();
        }
        curdisp = midlet.getDisplay().getCurrent();
        midlet.getDisplay().setCurrent(this);

        bottom_reached =false;

        offset = 0;
        repaint();

        exit = new Command(str_afsl_short[HelpDiabetes.languageIndex],str_afsl[HelpDiabetes.languageIndex],Command.EXIT,1);
        this.addCommand(exit);
        ok = new Command(str_ok[HelpDiabetes.languageIndex],Command.OK,1);
        this.setCommandListener(this);

    }
    protected void paint(Graphics graphics) {
        //setscrsize();
        graphics.setFont(userfont);
        graphics.setColor(255,255,255);
        graphics.fillRect(0, 0, scrwidth, scrhght);
        x = 0;
        y= offset;
        paintstring(("HelpDiabetes" ).getBytes(), ("HelpDiabetes" ).length(), false, graphics, userfont);
        y = y + fonthght;x = 0;
        paintstring((str_licenseagreement[HelpDiabetes.languageIndex]).getBytes(), ( str_licenseagreement[HelpDiabetes.languageIndex]).length(), false, graphics, userfont);
        y = y + fonthght;x = 0;
        y = y + fonthght;x = 0;
        paintstring(str_startmessage1[HelpDiabetes.languageIndex].getBytes(),str_startmessage1[HelpDiabetes.languageIndex].length(),false,graphics,userfont,true);
        y = y + fonthght;x = 0;
        y = y + fonthght;x = 0;
        paintstring(str_startmessage2[HelpDiabetes.languageIndex].getBytes(),str_startmessage2[HelpDiabetes.languageIndex].length(),false,graphics,userfont,true);
        y = y + fonthght;x = 0;
        paintstring(str_startmessage3[HelpDiabetes.languageIndex].getBytes(),str_startmessage3[HelpDiabetes.languageIndex].length(),false,graphics,userfont,true);
        y = y + fonthght;x = 0;
        y = y + fonthght;x = 0;
        paintstring(str_startmessage4[HelpDiabetes.languageIndex].getBytes(),str_startmessage4[HelpDiabetes.languageIndex].length(),false,graphics,userfont,true);
        y = y + fonthght;x = 0;
        paintstring(str_startmessage5[HelpDiabetes.languageIndex].getBytes(),str_startmessage5[HelpDiabetes.languageIndex].length(),false,graphics,userfont,true);
        y = y + fonthght;x = 0;
        paintstring(str_startmessage6[HelpDiabetes.languageIndex].getBytes(),str_startmessage6[HelpDiabetes.languageIndex].length(),false,graphics,userfont,true);
        if (!(y + fonthght < scrhght)) {
            //the text was not completely shown on the screen
            //start by erasing the last line
            graphics.setColor(255,255,255);
            graphics.fillRect(0,scrhght-fonthght,scrwidth,scrhght);
            //now draw a small triangle pointing downward to show there's more text
            graphics.setColor(0,0,0);
            graphics.fillTriangle(scrwidth/2-5, scrhght-fonthght, scrwidth/2+5, scrhght-fonthght, scrwidth/2,scrhght);

        } else {//text completely shown, 'ok' button can be added.
            this.addCommand(ok);
            start_message_completely_shown = true;
            bottom_reached=true;
        }

    }

    public void commandAction(Command c, Displayable d) {
        if (c == ok) {
            //switch back to getitemfromuser
            midlet.getDisplay().setCurrent(curdisp);
            //now open foodfile, this hasn't been done yet
            try {
                HelpDiabetes.foodfileinst = new FoodFile();
            } catch (Exception e) {

            }
        } else {//user pressed exit
            midlet.exitMIDlet();
        }
    }

    public synchronized void keyPressed(int keyCode) {
        if (getGameAction(keyCode)== UP) {
            if (offset < 0) {
                offset = offset + fonthght;
                bottom_reached=false;
            }
        } else if (getGameAction(keyCode) == DOWN) {
            if (!bottom_reached) {
                offset = offset - fonthght;
            }
        }
    }

    String GetClassName() {
        return("InitialScreen");
    }

}
